<?php
class AppController extends Controller
{
	var $components  = array('othAuth'); // necessary, we need to have the othauth component so it can do it's business logic
	var $helpers = array('OthAuth','Html','Javascript','Session'); // html is always needed, othauth helper is not a must, but you can do some cool things with it (see later on)
	var $othAuthRestrictions = array( 'add','edit','delete','index','send');  // these are the global restrictions, they are very important. all the permissions defined above are weighted against these restrictions to calculate the total allow or deny for a specific request.

    function beforeFilter()
    {
        $this->checkSession();
		$auth_conf = array(
					'mode'  => 'oth',
					'login_page'  => '/users/login',
					'logout_page' => '/users/logout',
					'access_page' => '/home/index',
					'hashkey'     => 'MySEcEeTHaSHKeYz',
					'noaccess_page' => '/users/noaccess',
					'strict_gid_check' => false);

		$this->othAuth->controller = &$this;
		$this->othAuth->init($auth_conf);
		$this->othAuth->check();
		
    }

    function checkSession()
    {
        // If the session info hasn't been set...
        /*if (!$this->Session->check('logou') || !$this->Session->check('Usuario'))
        {
            $_SESSION['mensagem']['error'][] = 'Você não está logado no sistema';
            // Force the user to login

            $this->redirect('/usuarios/login');
            exit();
        }*/
    }

	function beforeRender() {

		$us = 'othAuth.'.$this->othAuth->hashkey;

		//die(pr($this));
		if ($this->name == 'Webs') {
			switch ($this->action) {
			case "front_home":
			case "front_videos_list":
			case "front_video_view":
			case "front_noticias_list":
			case "front_noticia_view":
			case "front_events_list":
			case "front_event_view":
			case "front_downloads_list":
			case "front_download_view":				
			case "front_atividades":
			case "front_apoio":
			case "front_perfil":
			case "front_downloads":
			case "front_albums_list":
			case "front_album_view":
			case "front_image_view":
				$this->layout = 'site';
				break;
			default:
				$this->layout = 'default';	
			}
		} else if ($this->layout != 'ajax' && $this->layout != 'email') { // does session exists
			if ($this->Session->valid() && $this->Session->check($us))  {
				$this->layout = 'default';
			}
			else {
				$this->layout = 'mainpage';
			}
		}

	}
}

